﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;

namespace Lawyer.Web.Admin
{
    public partial class admin_archivelist : System.Web.UI.Page
    {
        public string status0 = "";
        public string status1 = "";
        public string status2 = "";
        public string status3 = "";
        public string status4 = "";
        public string statusall = "";
        Lawyer.BLL.ArchiveInfo bll = new Lawyer.BLL.ArchiveInfo();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                //btnDelete.Attributes.Add("onclick", "return confirm(\"你确认要删除吗？\")");
                BindData();

            }
            if (gridView.HeaderRow != null)
                gridView.HeaderRow.TableSection = TableRowSection.TableHeader;
        }
        #region gridView
        public void BindData()
        {
            #region
            //if (!Context.User.Identity.IsAuthenticated)
            //{
            //    return;
            //}
            //AccountsPrincipal user = new AccountsPrincipal(Context.User.Identity.Name);
            //if (user.HasPermissionID(PermId_Modify))
            //{
            //    gridView.Columns[6].Visible = true;
            //}
            //if (user.HasPermissionID(PermId_Delete))
            //{
            //    gridView.Columns[7].Visible = true;
            //}
            #endregion
            int status = -1;
            try
            {
                status = Request["status"] == null ? -1 : Int32.Parse(Request["status"].ToString());
            }
            catch { }
            
            string strWhere = "";

            if (status > 5 || status < 0)
            {

            }
            else {
                
                strWhere = " STATUS = " + status;
                statusall = "current";
            }
            if (status == 0) {
                status0 = "current";
            }
            else if (status == 1)
            {
                status1 = "current";
            }
            else if (status == 2)
            {
                status2 = "current";
            }
            else if (status == 3)
            {
                status3 = "current";
            }
            else if (status == 4)
            {
                status4 = "current";
            }
            List<Lawyer.Model.ArchiveInfo> list = bll.GetModelList(strWhere.ToString());

            //if (txtKeyword.Text.Trim() != "")
            //{      
            //    #warning 代码生成警告：请修改 keywordField 为需要匹配查询的真实字段名称
            //    //strWhere.AppendFormat("keywordField like '%{0}%'", txtKeyword.Text.Trim());
            //}            


            gridView.DataSource = list;
            gridView.DataBind();
        }

        protected void gridView_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            gridView.PageIndex = e.NewPageIndex;
            BindData();
        }
        protected void gridView_OnRowCreated(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                //e.Row.Cells[0].Text = "<input id='Checkbox2' type='checkbox' onclick='CheckAll()'/><label></label>";
                
            }
        }
        protected void gridView_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            // e.Row.Attributes.Add("style", "background:#FFF");
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                //LinkButton linkbtnDel = (LinkButton)e.Row.FindControl("LinkButton1");
                //linkbtnDel.Attributes.Add("onclick", "return confirm(\"你确认要删除吗\")");

                //object obj1 = DataBinder.Eval(e.Row.DataItem, "Levels");
                //if ((obj1 != null) && ((obj1.ToString() != "")))
                //{
                //    e.Row.Cells[1].Text = obj1.ToString();
                //}
                //string archiveid = e.Row.Cells[0].Text.ToString();
                Lawyer.Model.ArchiveInfo model = (Lawyer.Model.ArchiveInfo)e.Row.DataItem;
                LinkButton lnkBtn = (LinkButton)e.Row.FindControl("lbnConfirm");
                Label lbStatus = (Label)e.Row.FindControl("lbStatus");
                LinkButton hpChooseLawyer = (LinkButton)e.Row.FindControl("hpChooseLawyer");
                if (model == null)
                {
                    return;
                }
                if (model.Status == 0)
                {
                    lnkBtn.Visible = true;
                    lnkBtn.CommandName = "Check";
                    hpChooseLawyer.Visible = true;
                }
                else if (model.Status == 1)
                {
                    hpChooseLawyer.Visible = true;
                }
                else if (model.Status == 2)
                {
                    /*lnkBtn.Text = "Finish";
                    lnkBtn.Visible = true;
                    lnkBtn.CommandName = "Finish";*/
                }
                else if (model.Status == 3)
                {
                    lnkBtn.Visible = true;
                    lnkBtn.CommandName = "Finish";
                }
                else if (model.Status == 4)
                {
                }


            }
        }

        protected void gridView_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //#warning 代码生成警告：请检查确认真实主键的名称和类型是否正确
            //int ID = (int)gridView.DataKeys[e.RowIndex].Value;
            //bll.Delete(ID);
            //gridView.OnBind();
        }

        private string GetSelIDlist()
        {
            string idlist = "";
            bool BxsChkd = false;
            for (int i = 0; i < gridView.Rows.Count; i++)
            {
                CheckBox ChkBxItem = (CheckBox)gridView.Rows[i].FindControl("DeleteThis");
                if (ChkBxItem != null && ChkBxItem.Checked)
                {
                    BxsChkd = true;
                    //#warning 代码生成警告：请检查确认Cells的列索引是否正确
                    if (gridView.DataKeys[i].Value != null)
                    {
                        idlist += gridView.DataKeys[i].Value.ToString() + ",";
                    }
                }
            }
            if (BxsChkd)
            {
                idlist = idlist.Substring(0, idlist.LastIndexOf(","));
            }
            return idlist;
        }


        #endregion

        protected void gridView_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName == "Check")
            {
                //int IndexNum = Int32.Parse(e.CommandArgument.ToString());
                //string archiveid = gridView.DataKeys[IndexNum].Value.ToString();
                string archiveid = e.CommandArgument.ToString();
                Lawyer.BLL.ArchiveInfo bll = new Lawyer.BLL.ArchiveInfo();
                Lawyer.Model.ArchiveInfo model = bll.GetModel(archiveid);
                if (model != null && model.Status == 0)
                {
                    model.Status = 1;
                    model.CheckedBy = Session["UserID"].ToString();
                    model.CheckedDate = DateTime.Now;
                    model.UpdatedBy = Session["UserID"].ToString();
                    model.UpdatedDate = DateTime.Now;
                    if (bll.Update(model))
                    {
                        Maticsoft.Common.MessageBox.ShowAndRedirect(this, "confirm success!", "admin_archivelist.aspx");
                        return;
                    }
                }
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "status wrong or other error!", "admin_archivelist.aspx");
            }
            else if (e.CommandName == "Finish")
            {
                string archiveid = e.CommandArgument.ToString();
                Lawyer.BLL.ArchiveInfo bll = new Lawyer.BLL.ArchiveInfo();
                Lawyer.Model.ArchiveInfo model = bll.GetModel(archiveid);
                if (model != null && (model.Status == 2 || model.Status == 3))
                {
                    model.Status = 4;
                    model.UpdatedBy = Session["UserID"].ToString();
                    model.UpdatedDate = DateTime.Now;
                    if (bll.Update(model))
                    {
                        //Maticsoft.Common.MessageBox.ShowAndRedirect(this, "confirm success!", "admin_archivelist.aspx");
                        Web.Common.MessageBox.ShowSuccess(this,"submit success");
                        return;
                    }
                }
                Web.Common.MessageBox.ShowFail(this, "status wrong or other error!");
            }
        }
    }
}
